package com.example.administrator.text;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Toast;

import com.uzmap.pkg.openapi.ExternalActivity;
import com.uzmap.pkg.openapi.Html5EventListener;
import com.uzmap.pkg.openapi.WebViewProvider;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.json.JSONException;
import org.json.JSONObject;

import app.MyApplication;
import data.SharedPreferencesUtils;
import util.AgentIp;
import util.HomeUtil;
import util.LogUtil;
import util.NetWorkstateUtil;
import util.StringUtil;
import util.Url;
import util.payUtil.PayUtil;
import view.diaLogView.ChoicePaymentDialog;
import view.diaLogView.CrowdRetunDialog;
import view.diaLogView.UploadDialog;
import view.diaLogView.UploadLogDialog;

import static data.SharedPreferencesUtils.getParam;

/**
 * 报障  和设备 扫码
 */
public class APICloud2Activity extends ExternalActivity {
    String mFwq;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        EventBus.getDefault().register(this);

        /**
         * 接收apicoud的信息  type == 222 时表示  打开原生二维码的页面  223 支付
         * 226 code = -2  重新登录  228 代码关闭了扫描授权
         * 338  打开日志
         */
        addHtml5EventListener(new Html5EventListener("app_equipment") {
            @Override
            public void onReceive(WebViewProvider webViewProvider, Object o) {
                LogUtil.eE("app3c","收到信息..."+o.toString());
                try {
                    JSONObject jsonObject = new JSONObject(o.toString());
                    switch (jsonObject.getInt("type")){
                        case 222:
                            Intent intent = new Intent(HomeActivity.mAction);
                            intent.putExtra("wifi","ZXing");
                            sendBroadcast(intent);
                            break;
                        case 223:
                            SharedPreferencesUtils.setParam(APICloud2Activity.this, PayUtil.PAY_COMMODITY,"套餐购买");
                            ChoicePaymentDialog choicePaymentDialog = new ChoicePaymentDialog(APICloud2Activity.this,o.toString());
                            choicePaymentDialog.show();
                            break;
                        case 226:
                            CrowdRetunDialog crowdRetunDialog = new CrowdRetunDialog(APICloud2Activity.this);
                            crowdRetunDialog.show();
                            break;
                        case 338:
                            uploadLuog();
                            break;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        removeAllHtml5EventListener();
        EventBus.getDefault().unregister(this);
    }

    /*** 订阅事件FirstEvent*/
    @Subscribe
    public void onEventMainThread(final String string) throws JSONException {
        LogUtil.eE("app3c","..onEventMainThread..."+string);
        if(string !=null && !string.equals("")){
            if(string.equals("vip")){
                JSONObject extra = new JSONObject();
                extra.put("type","closeWin");
                sendEventToHtml5("NativeData",extra);
                HomeUtil.getHemeUtilNew().getHandlerNew().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        int deviceCount = (int) SharedPreferencesUtils.getParam(APICloud2Activity.this, HomeUtil.mPackage_deviceCount,1);
                        JSONObject extra1 = new JSONObject();
                        try {
                            extra1.put("type","updateEquipment");
                            extra1.put("deviceCount",deviceCount);
                            sendEventToHtml5("NativeData",extra1);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },2000);
            }else if(string.equals("uploadLog")){
                sendEventToHtml5("uploadLog",new JSONObject("ok"));
            }
        }
    }

    /**
     * 传日志
     */
    private void uploadLuog(){
        mFwq = (String) getParam(this,HomeUtil.mFWQ,"");
        if(mFwq.equals("")){
            mFwq = MyApplication.getApp().getFWQString();
        }
        if(MyApplication.getApp().getSiteAreaid() >=10000){
            final UploadDialog uploadDialog = new UploadDialog(this,"连接中");
            uploadDialog.show();
            HomeUtil.getHemeUtilNew().domainUtil(this,2000,new HomeUtil.OnHomeUtilClickListener() {
                @Override
                public void onItemClick(String type) {
                    if(type.equals("no")){
                        NetWorkstateUtil.getInstance().showNetworking(APICloud2Activity.this, new NetWorkstateUtil.NetworkingOnClickListener() {
                            @Override
                            public void onItemClick(boolean state) {
                                uploadDialog.dismiss();
                                if(state){
                                    Url.getUrlNew().setNETWORKING_ATATE(1);
                                }
                                UploadLogDialog noNetDialog = new UploadLogDialog(APICloud2Activity.this,StringUtil.getStringUtilNew().uploadHead(mFwq));
                                noNetDialog.show();
                            }
                        });
                    }else {
                        uploadDialog.dismiss();
                        final String url = StringUtil.getStringUtilNew().uploadHead(MyApplication.getApp().getFWQString());
                        UploadLogDialog noNetDialog = new UploadLogDialog(APICloud2Activity.this,url);
                        noNetDialog.show();
                    }
                }
            });
        }else {
            final String url = StringUtil.getStringUtilNew().uploadHead(MyApplication.getApp().getFWQString());
            UploadLogDialog noNetDialog = new UploadLogDialog(this,url);
            noNetDialog.show();
        }
    }
}
